/**
 * Created by L on 2017/5/24.
 */
var url="http://47.93.233.182/";
var zz=0;
window.onload=function () {
    //获取电影
    getMovie();
};

//请求数据
function getMovie() {
    $.ajax({
        url:url+"/getMovie",
        type:'GET',
        dataType:'JSON',
        success:function (text) {
            //数据操作
            var i=0;
            dataOpertion(text,i);
            //视频轮播器
            movieList(text);
            //剧照
            var images2=text.data[0].movie_image.split("+");
            requestPhoto(images2);
        },
        error:function () {

        }
    });
}


//剧照
function requestPhoto(text) {
    var qwe=document.getElementsByClassName("qwe")[0];

    if(text.length>=5) {
        $(".qwe>img").remove("img");
        for (var m = 0; m < text.length-1; m++) {
            var tup = document.createElement("img");
            tup.setAttribute("class", "tup");
            var img1 = text[m];
            tup.setAttribute("src", url + img1);
            qwe.appendChild(tup);
        }
    }

    photoList();

}

//视频div创建
function movieList(text) {
    var qwe1=document.getElementsByClassName("qwe1")[0];
    for(var m=0;m<text.data.length;m++){
        var div=document.createElement("div");
        div.setAttribute("class","tup1");
        qwe1.appendChild(div);

        var img1=text.data[m].movie_image.split("+");
        var div2=document.getElementsByClassName("tup1")[m];
        var img=document.createElement("img");
        img.setAttribute("src",url+img1[0]);
        img.setAttribute("class","moviePhotos");
        div2.appendChild(img);

        //绑定点击事件
        (function () {
            var mm=m;
            document.getElementsByClassName("moviePhotos")[mm].onclick=function () {
                dataOpertion(text,mm);
                zz=mm;
                var src=text.data[mm].vedio_id;
                $("#bf1").attr("value",src[1]);
                //剧照轮播器

                var images1=text.data[mm].movie_image.split("+");
                requestPhoto(images1);

            }
        })();

        var ewq1=document.getElementsByClassName("tup1")[m];
        var span=document.createElement("span");
        span.setAttribute("class","bofang");
        ewq1.appendChild(span);
    }

    lunboqi(text);
    bofangqi(text);

}

//操作请求的数据
function dataOpertion(text,i){
    $("#movieName").html(text.data[i].movie_name);
    $("#movieShowTime").html(text.data[i].push_time);
    $("#movieLong").html(text.data[i].movie_long);
    $("#movieWriters").html(text.data[i].movie_writer);
    $("#movieClass").html(text.data[i].movie_type);
    $("#movieProducer").html(text.data[i].movie_producer);
    $("#duanhang").html(text.data[i].movie_company);
    var img=text.data[i].movie_image.split("+");
    $("#pt1").attr('src',url+text.data[i].video_poster);
    $("#words").html(text.data[i].movie_introduce);

    //写个正则
    var images=$("#words img");
    for(var i=0;i<images.length;i++){
        var src=images.eq(i).attr("src");
        images.eq(i).attr("src",url+src);
    }


}

//剧照轮播器
function photoList() {
    var index=0;
    var index1=1;
    var index2=2;
    var index3=3;
    var index4=4;
    var bannerimg=$(".tup");
    for(var i=0;i<bannerimg.length;i++){
        bannerimg.eq(i).css({
            left:233*i+'px'
        })
    }
    $("#left").click(function (){

        if(!$(".tup").is(":animated")){

            if(index>=bannerimg.length){
                index=0;
            }
            if(index4>=bannerimg.length){
                index4=0;
            }
            if(index3>=bannerimg.length){
                index3=0;
            }
            if(index2>=bannerimg.length){
                index2=0;
            }
            if(index1>=bannerimg.length){
                index1=0;
            }
            bannerimg.eq(index).animate({
                left:'-=233px'
            });
            bannerimg.eq(index1).animate({
                left:'-=233px'
            });
            bannerimg.eq(index2).animate({
                left:'-=233px'
            });
            bannerimg.eq(index3).animate({
                left:'-=233px'
            });
            bannerimg.eq(index4).css({
                left:'700px'
            });

            index=index+1;
            index1=index1+1;
            index2=index2+1;
            index3=index3+1;
            index4=index4+1;
        }
    });
    $("#right").click(function () {
        if(!$(".tup").is(":animated")){

            index=index-1;
            index1=index1-1;
            index2=index2-1;
            index3=index3-1;
            index4=index4-1;
//6张应该是从-233px到显示0px

            if(index<0){
                index=bannerimg.length-1;

            }
            if(index1<0){
                index1=bannerimg.length-1;

            }
            if(index2<0){
                index2=bannerimg.length-1;

            }
            if(index3<0){
                index3=bannerimg.length-1;

            }
            if(index4<0){
                index4=bannerimg.length-1;

            }

            bannerimg.eq(index).css({
                left:'-233px'
            });
            bannerimg.eq(index).animate({
                left:'+=233px'
            });
            bannerimg.eq(index1).animate({
                left:'+=233px'
            });
            bannerimg.eq(index2).animate({
                left:'+=233px'
            });
            bannerimg.eq(index3).animate({
                left:'+=233px'
            });
            bannerimg.eq(index4).animate({
                left:'+=233px'
            });


        }

    })
}

//电影轮播器
function lunboqi(){

    var index=0;
    var index1=1;
    var index2=2;
    var index3=3;

    var bannerimg=$(".tup1");
    for(var i=0;i<bannerimg.length;i++){
        bannerimg.eq(i).css({
            left:350*i+'px'
        })
    }
    $("#left1").click(function (){

        if(!$(".tup1").is(":animated")){

            if(index>=bannerimg.length){
                index=0;
            }

            if(index3>=bannerimg.length){
                index3=0;
            }
            if(index2>=bannerimg.length){
                index2=0;
            }
            if(index1>=bannerimg.length){
                index1=0;
            }
            bannerimg.eq(index).animate({
                left:'-=350px'
            });
            bannerimg.eq(index1).animate({
                left:'-=350px'
            });
            bannerimg.eq(index2).animate({
                left:'-=350px'
            });
            bannerimg.eq(index3).css({
                left:'700px'
            });
            index=index+1;
            index1=index1+1;
            index2=index2+1;
            index3=index3+1;
        }
    });
    $("#right1").click(function () {
        if(!$(".tup1").is(":animated")){
            index=index-1;
            index1=index1-1;
            index2=index2-1;
            index3=index3-1;

//6张应该是从-233px到显示0px

            if(index<0){
                index=bannerimg.length-1;

            }
            if(index1<0){
                index1=bannerimg.length-1;

            }
            if(index2<0){
                index2=bannerimg.length-1;

            }
            if(index3<0){
                index3=bannerimg.length-1;

            }


            bannerimg.eq(index).css({
                left:'-350px'
            });
            bannerimg.eq(index).animate({
                left:'+=350px'
            });
            bannerimg.eq(index1).animate({
                left:'+=350px'
            });
            bannerimg.eq(index2).animate({
                left:'+=350px'
            });
            bannerimg.eq(index3).animate({
                left:'+=350px'
            });

        }

    })
}

//播放时禁止滚轮滚动
function gunlun() {
    event.preventDefault()
}

//关闭时取消禁止滚轮滚动
function gundong() {
    document.body.removeEventListener("mousewheel",gunlun)
}

//点击播放视频
function bofangqi(text){



        for(var i=0;i<=$(".bofang").length-1;i++){

            (function () {
                var b=i;
                document.getElementsByClassName("bofang").item(i).onclick=function () {
                    $("#bofangqi").css("display","block");
                    var movie=text.data[b].vedio_id;
                    $("#bofangshiping").html(movie);
                    document.body.addEventListener("mousewheel",gunlun);
                    $("#mengban").css({
                        "transform":"Scale(1)",
                        "opacity":"1"
                    });
                }

            })()

        }


        //点击播放电影
        $("#bf1").click(function () {
            $("#bofangqi").css("display","block");
            $("#mengban").css("display","block");
            var movie1=text.data[zz].vedio_id;
            $("#bofangshiping").html(movie1);
            document.body.addEventListener("mousewheel",gunlun);
            $("#mengban").css({
                "transform":"Scale(1)",
                "opacity":"1"
            });

        });



        //点击关闭电影播放
        $("#bofangqi").click(function () {
            $("#bofangqi").css("display","none");

            $("#bofangshiping").html("");
            //  document.getElementById("my_video_1_html5_api").src="http://123.207.241.214/wangku/test.mp4";
            $("#mengban").css({
                "transform":"Scale(0.001)",
                "opacity":"0"
            });
            gundong();

        })

}

